<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel=StyleSheet HREF="../stylesheet/stylesheet.css" TYPE="text/css"
	MEDIA=screen>
	<title>-</title>
<link rel="stylesheet" href="stylereport.css" type="text/css" />
<link rel="shortcut icon" href="../image/Letter-T.ico" />
</head>
<body>
<?php
include("../connect/connect.php");
include("../function/thaidate.php");

if($_POST['type']!="" && $_POST['dateInput']!="" && $_POST['dateInput2']!=""){
	$dmy = $_POST["dateInput"];
	$d = substr($dmy, 0, 2);
	$m = substr($dmy, 3, 2);
	$y = substr($dmy, 6, 4);
	$date = ($y - 543) . '-' . $m . '-' . $d;
	//$date = date("Y-m-d", $adate);
	

	$dmy2 = $_POST["dateInput2"];
	$d2 = substr($dmy2, 0, 2);
	$m2 = substr($dmy2, 3, 2);
	$y2 = substr($dmy2, 6, 4);
	$adate2 = strtotime("+1 day",strtotime(($y2 - 543) . '-' . $m2 . '-' . $d2));
	$date2 = date("Y-m-d", $adate2);
	
	$Query = mysql_query("SELECT debtor_total_id,debtor_total_date,debtor_total_name FROM debtor_total WHERE debtortype_id = '".$_POST['type']."' AND debtor_total_date3 BETWEEN '$date' AND '$date2' GROUP BY debtor_total_name  ORDER BY debtor_total_name");
}
?>
<h1>รายงานลูกหนี้ <?php if($_POST['type']=='1'){ echo "พนักงาน";} if($_POST['type']=='2'){ echo "อื่นๆ";} ?></h1>
<h2>ตั้งแต่วันที่ <?php echo thai_date(strtotime($date))." ถึงวันที่ ". thai_date(strtotime("-1 day",strtotime($date2))); ?></h2>
<?php
$i=0;
while($row=mysql_fetch_array($Query)){
	$i++;
?>
<br /><font size="4"><strong><?php echo $row['debtor_total_name']; ?></strong></font><br /><br />
<table class="listorder">
  <thead>
<tr>
<td width="5%">ลำดับ</td>
<td width="20%">วันที่</td>
<td>รายการ</td>
<td width="10%">หน่วยละ</td>
<td width="10%">จำนวน</td>
<td width="15%">ราคาขาย</td>
<td width="15%">ราคา + Sv.</td>
</tr>
  </thead>
<?php
$num1 = 0;
	$dQuery = mysql_query("SELECT product.prod_name as prod_name,debtor_total.*,debtor.prod_id,sum(debtor.sale_qty) as sale_qty,debtor.sale_price,debtor.sale_discount FROM debtor_total,debtor,product WHERE debtor_total.debtortype_id = '".$_POST['type']."' AND product.prod_id=debtor.prod_id AND debtor_total.debtor_total_name = '".$row['debtor_total_name']."' AND debtor_total.debtor_total_date3 BETWEEN '$date' AND '$date2' AND debtor_total.debtor_total_id=debtor.debtor_total_id GROUP BY debtor_total.debtor_total_name,debtor.sale_discount,debtor.prod_id,debtor.sale_price,day(debtor_total.debtor_total_date3),month(debtor_total.debtor_total_date3),year(debtor_total.debtor_total_date3) ORDER BY debtor_total.debtor_total_date3");
	while ($dRow = mysql_fetch_array($dQuery)){
	$num1++;
?>
<tr>
<td align="center"><?php echo $num1; ?></td>
<td align="center"><?php $ddate = strtotime($dRow['debtor_total_date3']); echo thai_date($ddate); ?></td>
<td align="left" style="padding-left:10px"><?php echo $dRow['prod_name']; ?></td>
<td align="right" style="padding-right:10px"><?php echo "".number_format($dRow['sale_price'], 2,'.',','); ?></td>
<td align="right" style="padding-right:10px"><?php echo $dRow['sale_qty']; ?></td>
<td align="right" style="padding-right:10px"><?php echo number_format($dRow['sale_price']*$dRow['sale_qty'], 2,'.',','); ?></td>
<td align="right" style="padding-right:10px"><?php $sale_discount1 = ($dRow['sale_price']*$dRow['sale_qty'])+$dRow['sale_discount'];
echo "".number_format($sale_discount1, 2,'.',',');
?></td>
</tr>
<?php
	$strTotal1[$i]=$strTotal1[$i]+$sale_discount1;
}
?>
<tfoot>
  <tr>
    <td colspan="6" align="center">รวมเงิน</td>
    <td align="right" style="padding-right:10px"><?php echo "".number_format($strTotal1[$i], 2,'.',','); ?></td>
	</tr>
</tfoot>
</table><br />
<?php } ?>
<br /><input type="button" name="btnPrint" value="พิมพ์รายงาน" onclick="JavaScript:window.print();" /> <input type="button" name="btnClose" value="ปิดหน้าต่าง" onclick="JavaScript:window.close();" /><br /><br />
</body>
</html>
